package com.fqgj.xjd.user.service.impl;

import com.fqgj.common.api.Response;
import com.fqgj.xjd.user.client.UserFrozenService;
import com.fqgj.xjd.user.client.enums.FrozenCodeEnum;
import com.fqgj.xjd.user.client.response.UserFrozen;
import com.fqgj.xjd.user.dao.UserFrozenCodeMapDao;
import com.fqgj.xjd.user.entity.UserFrozenCodeMapEntity;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component("userFrozenService")
public class UserFrozenServiceImpl implements UserFrozenService {

    @Autowired
    private UserFrozenCodeMapDao userFrozenCodeMapDao;

    @Override
    public List<UserFrozen> getFrozenCode(String userCode){
        List<UserFrozenCodeMapEntity> userFrozenCodeMapEntityList = userFrozenCodeMapDao.selectUserFrozenCodeMapByUserCode(userCode);
        List<UserFrozen> frozenList = userFrozenCodeMapEntity2UserFrozenList(userFrozenCodeMapEntityList);

        return frozenList;
    }

    @Override
    public Response<List<UserFrozen>> getFrozenCodeByUserCode(String userCode) {
        List<UserFrozenCodeMapEntity> userFrozenCodeMapEntityList = userFrozenCodeMapDao.selectUserFrozenCodeMapByUserCode(userCode);
        List<UserFrozen> frozenList = userFrozenCodeMapEntity2UserFrozenList(userFrozenCodeMapEntityList);
        return Response.ok().putData(frozenList);
    }

    /**
     * 根据用户code、categorycode查询冷冻列表
     * @param userCode
     * @param categoryCode
     * @return
     */
    @Override
    public List<UserFrozen> getFrozenCodeByUserCodeAndCategoryCode(String userCode, String categoryCode) {
        List<UserFrozenCodeMapEntity> userFrozenCodeMapEntities = userFrozenCodeMapDao.selectUserFrozenCodeMapByUserCodeAndCategoryCode(userCode, categoryCode);
        List<UserFrozen> frozenList = userFrozenCodeMapEntity2UserFrozenList(userFrozenCodeMapEntities);
        return frozenList;
    }

    private List<UserFrozen> userFrozenCodeMapEntity2UserFrozenList(List<UserFrozenCodeMapEntity> userFrozenCodeMapEntities){
        List<UserFrozen> frozenList = null;
        if(CollectionUtils.isNotEmpty(userFrozenCodeMapEntities)){
            frozenList = new ArrayList<UserFrozen>();
            for(UserFrozenCodeMapEntity userFrozenCodeMapEntity:userFrozenCodeMapEntities){
                FrozenCodeEnum frozenCodeEnum = FrozenCodeEnum.getEnumByCode(userFrozenCodeMapEntity.getFrozenCode());
                if (frozenCodeEnum == null) {
                    continue;
                }
                //过期
                if(userFrozenCodeMapEntity.getFrozenEndDate().before(new Date())){
                    continue;
                }
                UserFrozen userFrozen = new UserFrozen();
                userFrozen.setCode(frozenCodeEnum.getCode())
                        .setDesc(frozenCodeEnum.getDesc())
                        .setFrozenEndDate(userFrozenCodeMapEntity.getFrozenEndDate())
                        .setCategoryCode(userFrozenCodeMapEntity.getCategoryCode());
                frozenList.add(userFrozen);
            }
        }
        return frozenList;
    }
}
